package scm;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class LogWindow extends JFrame
{
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JTextField jTextField1;
    private JPasswordField jPasswordField1;
    private JButton jButton1;
    private JPanel contentPane;
    private Connection c = null;
    private Scm main = null;

    public LogWindow(Scm main)
    {
        super();
        this.main = main;
	create();
        this.setVisible(true);
    }


    private void create()
    {
        jLabel1 = new JLabel();
	jLabel2 = new JLabel();
	jTextField1 = new JTextField();
	jPasswordField1 = new JPasswordField();
	jButton1 = new JButton();
	contentPane = (JPanel)this.getContentPane();

	jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
	jLabel1.setForeground(new Color(0, 0, 255));
	jLabel1.setText("Login:");

	jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
	jLabel2.setForeground(new Color(0, 0, 255));
	jLabel2.setText("Hasło:");

	jTextField1.setForeground(new Color(0, 0, 255));
	jTextField1.setSelectedTextColor(new Color(0, 0, 255));
	jTextField1.setToolTipText("Podaj nazwę użytkownika");

	jPasswordField1.setForeground(new Color(0, 0, 255));
	jPasswordField1.setToolTipText("Podaj hasło");

	jButton1.setBackground(new Color(204, 204, 204));
	jButton1.setForeground(new Color(0, 0, 255));
	jButton1.setText("Zaloguj");
	jButton1.addActionListener(new ActionListener()
        {
	    public void actionPerformed(ActionEvent e)
	    {
		jButton1_actionPerformed(e);
	    }

	});
		
	contentPane.setLayout(null);
	contentPane.setBorder(BorderFactory.createEtchedBorder());
	contentPane.setBackground(new Color(204, 204, 204));
	addComponent(contentPane, jLabel1, 5,10,106,18);
	addComponent(contentPane, jLabel2, 5,47,97,18);
	addComponent(contentPane, jTextField1, 110,10,183,22);
	addComponent(contentPane, jPasswordField1, 110,45,183,22);
	addComponent(contentPane, jButton1, 150,75,83,28);

	this.setTitle("Uwierzytelnienie");
	this.setLocation(new Point(250,250));
	this.setSize(new Dimension(335, 141));
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	this.setResizable(false);
    }


    private void addComponent(Container container,Component c,int x,int y,int width,int height)
    {
	c.setBounds(x,y,width,height);
	container.add(c);
    }


    private void jButton1_actionPerformed(ActionEvent e)
    {
        String username = new String(jTextField1.getText());
	String password = new String(jPasswordField1.getText());

	if(username.equals("") || password.equals(""))
	{
	    jButton1.setEnabled(false);
	    JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>Musisz podać nazwę użytkownika i hasło</FONT></HTML>");
	    JOptionPane.showMessageDialog(null,errorFields);
	    jTextField1.setText(username);
	    jPasswordField1.setText(password);
	    jButton1.setEnabled(true);
	    this.setVisible(true);
	}
        else if (password.length() < 6)
        {
            jButton1.setEnabled(false);
	    JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>Hasło musi mieć co najmniej 6 znaków.</FONT></HTML>");
	    JOptionPane.showMessageDialog(null,errorFields);
	    jTextField1.setText(username);
	    jPasswordField1.setText(password);
	    jButton1.setEnabled(true);
	    this.setVisible(true);
        }
        else
        {
             try
             {
                 Class.forName("org.postgresql.Driver");
             }
             catch (ClassNotFoundException cnfe)
             {
                 JOptionPane.showMessageDialog(null, cnfe.getMessage());
             }
             try
             {
                 c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

                 PreparedStatement ps = c.prepareStatement("SELECT login, password FROM users WHERE login = ? AND password = ?");
                 ps.setString(1, username);
                 ps.setString(2, password);
                 ResultSet rs = ps.executeQuery();
                 boolean jest = false;
                 while (rs.next())
                 {
                     if (rs.getString("login").equals(username) && rs.getString("password").equals(password))
                     {
                         JOptionPane.showMessageDialog(null, "Witaj " + username + "\n");
                         jest = true;
                         this.main.AppItems[1].setVisible(false);
                         this.main.AppItems[2].setVisible(true);
                         this.dispose();
                         break;
                     }
                 }

                 if (!jest)
                 {
                     jButton1.setEnabled(false);
	             JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>System nie rozpoznaję Cię!</FONT></HTML>");
                     JOptionPane.showMessageDialog(null,errorFields);
	             jTextField1.setText(username);
	             jPasswordField1.setText(password);
	             jButton1.setEnabled(true);
	             this.setVisible(true);
                 }
                 else
                 {
                      for (JMenuItem jmi : main.SettingItems)
                      {
                           jmi.setVisible(true);

                      }
                      main.AppItems[2].setVisible(true);
                 }
             }
             catch (SQLException se)
             {
                 JOptionPane.showMessageDialog(null, se.getMessage());
             }
        }
    }

}